/**
 * ECN信息Info*E Task类
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.integration.oa.task;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import wt.change2.WTChangeOrder2;

import com.infoengine.SAK.Task;
import com.infoengine.object.factory.Group;
import com.infoengine.util.IEException;

import ext.generic.erp.attributes.AttributeUtil;
import ext.generic.erp.common.CommonUtil;
import ext.generic.erp.task.IETaskCommonUtil;
import ext.generic.erp.util.IEXMLUtil;

public class ECNIETask {
	private static final String RELEASE_FLAG = "flag" ;
	private static final String RELEASE_DATE = "releaseDate" ;
	private static final String FD_ID = "FD_ID";
	
	//PDM发布数据成功
	public static final String PDM_RELEASE_STATUS = "1" ;
	
	private static String ieXMLFileQuery = "ext/generic/integration/oa/ecn/ECNQuery.xml" ;
	private static String ieXMLFileCreate = "ext/generic/integration/oa/ecn/ECNCreate.xml" ;
	private static String ieXMLFileUpdate = "ext/generic/integration/oa/ecn/ECNUpdate.xml" ;
	private static String ieXMLFileDelete = "ext/generic/integration/oa/ecn/ECNDelete.xml" ;
	
	private static IEXMLUtil ieXMLUtil = new IEXMLUtil();
	
	public static boolean hasObject( WTChangeOrder2 ecn ){
		System.out.println(">>>>> Start hasObject Method ...... ") ;
		
		boolean hasObject = false ;
		
		Group group = query( ecn ) ;
		
		if( group != null && group.getElementCount() > 0 ){
			hasObject = true ;
		}
		
		System.out.println(">>>>> hasObject Method , hasObject = " + hasObject ) ;
		
		System.out.println(">>>>> End hasObject Method ...... ") ;
		
		return hasObject ;
	}
	
	public static Group query(WTChangeOrder2 ecn){
		System.out.println(">>>>> Start query Method ...... ") ;
		
		Group group = null ;
		
		if( ecn == null ){
			return group ;
		}
		
		//创建任务
		Task task = new Task( ieXMLFileQuery );

		addQueryWhere(ecn, task) ;
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//获取返回值
		group =  task.getGroup("QueryObjects");
		
		if( group == null ){
			System.out.println(">>>>> Query , Group == null") ;
		}else{
			System.out.println(">>>>> Query , Group Size = " + group.getElementCount() ) ;
		}
		
		System.out.println(">>>>> End query Method ...... ") ;
		
		return group ;
	}

	public static void add( WTChangeOrder2 ecn ){
		add( ecn , "") ;
	}
	
	public static void add( WTChangeOrder2 ecn , String releaseTime ){		
		if( hasObject( ecn ) ){
			update( ecn , releaseTime ) ;
		}else{
			create( ecn , releaseTime ) ;
		}
	}
	
	public static void delete( WTChangeOrder2 ecn ){
		System.out.println(">>>>> Start delete Method ...... ") ;
		
		if( ecn == null ){
			return ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileDelete);
		
		addQueryWhere( ecn , task);
		
		try {
			task.invoke();
		} catch (IEException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
		
		}
		
		System.out.println(">>>>> End delete Method ...... ") ;
	}
	
	protected static void create( WTChangeOrder2 ecn , String releaseTime ){
		System.out.println(">>>>> Start create Method ...... ") ;
		
		if( ecn == null ){
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml(IETaskCommonUtil.TASK_FILE_DIRECOTRY + ieXMLFileCreate );
		
		Task task = new Task( ieXMLFileCreate );

		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			System.out.println( "Debug   attributeId:"+ attributeId );
			//新增属性"FD_ID"
			if( attributeId.trim().equals( FD_ID ) ){
				String fdAttributeValue = AttributeUtil.getAttribute(ecn, "number" ) ;
				
				task.addParam( attributeId , fdAttributeValue );
				continue;
			}
			
			String attributeValue = AttributeUtil.getAttribute(ecn, attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		addProcessStatus( task , releaseTime ) ;
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
		
		}
		
		System.out.println(">>>>> End create Method ...... ") ;
	}
	
	protected static void update( WTChangeOrder2 ecn , String releaseTime ){
		System.out.println(">>>>> Start update Method ...... ") ;
		
		if( ecn == null ){
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml(IETaskCommonUtil.TASK_FILE_DIRECOTRY + ieXMLFileUpdate );

		//创建任务
		Task task = new Task(ieXMLFileUpdate);
		
		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			
			String attributeValue = AttributeUtil.getAttribute(ecn, attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		addProcessStatus( task , releaseTime );

		addQueryWhere( ecn , task ) ;

		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
		}
		
		System.out.println(">>>>> End update Method ...... ") ;
	}
	
	private static void addQueryWhere(WTChangeOrder2 ecn, Task task) {
		if( ecn == null || task == null ){
			return ;
		}
		
		String where = "ecn_id ='" + AttributeUtil.getAttribute( ecn , "number") + "' " ;
		
		task.addParam( "WHERE",where );
	}
	
	private static void addProcessStatus(Task task , String releaseTime){
		if( task != null ){
			try{
				task.removeParam( RELEASE_FLAG ) ;
				task.removeParam( RELEASE_DATE ) ;
				
				task.addParam( RELEASE_FLAG , PDM_RELEASE_STATUS) ;
				
				if(releaseTime == null || releaseTime.trim().equals("")){
					releaseTime = CommonUtil.getCurrentTime() ;
				}
				
				task.addParam( RELEASE_DATE , releaseTime ) ;
			}catch(Exception e){
				e.printStackTrace() ;
			}finally{
				
			}
		}
	}
}
